<?php

namespace App\Service;

use App\Model\Redis\Redis;

class AccessTokenRedis
{
    public static function get()
    {
        $config = config('MINI');
        $redisKey = RedisKeyService::accessToken($config['appid']);
        if (empty(Redis::getInstance()->exists($redisKey))) {
            $res = self:: accessToken($config['appid'], $config['appsecret']);
            $res = json_decode($res, true);
            $accessToken = $res['access_token'];
            Redis::getInstance()->set($redisKey, $accessToken, 7000);
        } else {
            $accessToken = Redis::getInstance()->get($redisKey);
        }
        return $accessToken;
    }

    static public function accessToken($appid, $appsecret)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";

        return getRequest($url);
    }
}